package com.zzq.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.zzq.dao.MemberMapper;
import com.zzq.dao.OrderMapper;
import com.zzq.dao.SetmealMapper;
import com.zzq.pojo.MemberExample;
import com.zzq.pojo.OrderExample;
import com.zzq.service.ReportService;
import com.zzq.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 幼儿猿大班
 * 2021/7/30
 */
@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public Map<String, Object> findMonthAndMemberCount() throws Exception {
        // 获取日历对象
        Calendar calendar=Calendar.getInstance();

        //获取一年前的日期对象
        calendar.add(Calendar.MONTH,-12);

        ArrayList<String> months = new ArrayList<>();
        ArrayList<Long> memberCount = new ArrayList<Long>();
        for (int i = 0; i < 12; i++) {
            calendar.add(Calendar.MONTH,1);
            String month = new SimpleDateFormat("yyyy-MM").format(calendar.getTime());
            String lastDayOfMonth = DateUtils.getLastDayOfMonth(month);
            months.add(month);
            // 根据month查会员数量
            long l = memberMapper.countByDate(lastDayOfMonth);
            memberCount.add(l);
        }

        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("months",months);
        stringObjectHashMap.put("memberCount",memberCount);
        return stringObjectHashMap;
    }

    @Override
    public Map<String, Object> findSetmealNameAndCount() {

        HashMap<String, Object> hashMap = new HashMap<>();
        ArrayList<String> setmealNames = new ArrayList<>();
        List<Map<String,Object>> list= setmealMapper.selectStemealCount();

        for (Map<String, Object> map : list) {
            setmealNames.add(map.get("name").toString());
        }
        hashMap.put("setmealNames",setmealNames);
        hashMap.put("setmealCount",list);
        return hashMap;
    }

    @Override
    public Map<String, Object> getBusiness() throws Exception {

        HashMap<String, Object> hashMap = new HashMap<>();

        //今天日期
        String today = DateUtils.parseDate2String(DateUtils.getToday());
        hashMap.put("reportDate",today);

        //本周第一天
        String theFirstDayOfWeek = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());

        //本月第一天
        String theFirstDayOfMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());

        // 今日新增会员数
        MemberExample memberExample = new MemberExample();
        memberExample.createCriteria().andRegtimeEqualTo(DateUtils.parseString2Date(today));
        long l = memberMapper.countByExample(memberExample);
        hashMap.put("todayNewMember",l);

        //总会员数
        long l1 = memberMapper.countByExample(null);
        hashMap.put("totalMember",l1);

        // 本周新增会员 thisWeekNewMember
        MemberExample memberExample1 = new MemberExample();
        memberExample1.createCriteria().andRegtimeGreaterThanOrEqualTo(DateUtils.parseString2Date(theFirstDayOfWeek));
        long l2 = memberMapper.countByExample(memberExample1);
        hashMap.put("thisWeekNewMember",l2);

        //  本月新增会员数  thisMonthNewMember
        MemberExample memberExample2 = new MemberExample();
        memberExample2.createCriteria().andRegtimeGreaterThanOrEqualTo(DateUtils.parseString2Date(theFirstDayOfMonth));
        long l3 = memberMapper.countByExample(memberExample2);
        hashMap.put("thisMonthNewMember",l3);

        //今日预约数   todayOrderNumber
        OrderExample orderExample = new OrderExample();
        orderExample.createCriteria().andOrderdateEqualTo(DateUtils.parseString2Date(today));
        long n1 = orderMapper.countByExample(orderExample);
        hashMap.put("todayOrderNumber",n1);

        //今日出游数   todayVisitsNumber
        OrderExample orderExample3 = new OrderExample();
        orderExample3.createCriteria().andOrderdateEqualTo(DateUtils.parseString2Date(today)).andOrderstatusEqualTo("已出游");
        long m1 = orderMapper.countByExample(orderExample3);
        hashMap.put("todayVisitsNumber",m1);

        //本周预约数   thisWeekOrderNumber
        OrderExample orderExample1 = new OrderExample();
        orderExample1.createCriteria().andOrderdateGreaterThanOrEqualTo(DateUtils.parseString2Date(theFirstDayOfWeek));
        long n2 = orderMapper.countByExample(orderExample1);
        hashMap.put("thisWeekOrderNumber",n2);

        //本周出游数   thisWeekVisitsNumber
        OrderExample orderExample4 = new OrderExample();
        orderExample4.createCriteria().andOrderdateGreaterThanOrEqualTo(DateUtils.parseString2Date(theFirstDayOfWeek)).andOrderstatusEqualTo("已出游");
        long m2 = orderMapper.countByExample(orderExample4);
        hashMap.put("thisWeekVisitsNumber",m2);

        //本月预约数  thisMonthOrderNumber
        OrderExample orderExample2 = new OrderExample();
        orderExample2.createCriteria().andOrderdateGreaterThanOrEqualTo(DateUtils.parseString2Date(theFirstDayOfMonth));
        long n3 = orderMapper.countByExample(orderExample2);
        hashMap.put("thisMonthOrderNumber",n3);

        //本月出游数  thisMonthVisitsNumber
        OrderExample orderExample5 = new OrderExample();
        orderExample5.createCriteria().andOrderdateGreaterThanOrEqualTo(DateUtils.parseString2Date(theFirstDayOfMonth)).andOrderstatusEqualTo("已出游");
        long m3 = orderMapper.countByExample(orderExample5);
        hashMap.put("thisMonthVisitsNumber",m3);

        //  hotSetmeal
        List<Map<String,Object>> list=setmealMapper.selectHotSetmeal();
        hashMap.put("hotSetmeal",list);

        return hashMap;
    }

}

